package Link;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;


/**
 * Session Bean implementation class VerifyLinkEJB
 * 
 * This session bean is controlling a string if it can be an url or not. It can be
 * used for an input validation. 
 * 
 * @autor Michael Moos
 * @author Knupp Reto
 */
@Stateless
@LocalBean
public class VerifyLinkEJB implements VerifyLinkEJBRemote, VerifyLinkEJBLocal 
{	
	/* Regular expression to find out if a string is an url or not */
	static final String regex = "^((https?|ftp)://|(www|ftp)\\.)[a-z0-9-]+(\\.[a-z0-9-]+)+([/?].*)?$";
	
    /**
     * Default constructor. 
     */
    public VerifyLinkEJB() 
    {
    	
    }

    /**
     * Verifies the given string if it is an url or not. This ejb is working with 
     * a regular expression for url's. 
     *  
     * That the string can be recognized as an url, the following rules are important: 
     * - The url can start with the following strings: "http://", "https://", "www."
     * - The url cant end with a "."
     * 
     * @param link String to verify
     * 
     * @return true: Link is an url / false: Link is not an url
     */
    public boolean verifyLinkEJB(String link)
    {
    	boolean resFlag;
    	
    	/* create pattern */
    	Pattern p = Pattern.compile(regex);
    	
    	/* get matcher */
    	Matcher m = p.matcher(link);
    	
    	/* try to find if there is a correct url */
    	if(m.find()){
    		/* it is an url */
    		resFlag = true;
    	} else {
    		/* it is not an url */
    		resFlag = false;
    	}
    	
    	return resFlag;
    }
}
